package com.calf.module.export.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.calf.cn.service.BaseManager;
import com.calf.module.export.entity.ExportOrderData;
import com.calf.module.export.entity.ExportPlayData;
import com.calf.module.export.entity.ExportUserData;
import com.calf.module.loan.entity.Customer;
import com.calf.module.loan.entity.DitchName;
import com.calf.module.loan.entity.UserDetails;
import com.calf.module.message.entity.Message;

/**
 * 导出文件
 * @author guixin
 *
 */
@Controller
@RequestMapping("/export")
public class ExportDataFileController {

	@Autowired
	private BaseManager baseManager;
	
	
	@RequestMapping(value="/home.htm",method=RequestMethod.GET)
	public String home() {
		return "export/exportfile";
	}
	
	/**
	 * 导出课程播放记录
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value="/play.htm",method=RequestMethod.GET)
	public void exportPlay(HttpServletResponse response,HttpServletRequest request) throws IOException{
		String begin_time = request.getParameter("begin_time");
		String end_time = request.getParameter("end_time");
		String app_name = request.getParameter("app_name");
		
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("begin_time", begin_time);
		parameters.put("end_time", end_time);
		
		OutputStream out = null;
		response.setContentType("text/plain");
		String fileName=String.format("%s_%s_%s_play.txt", begin_time,end_time,app_name);
        response.setHeader("Content-disposition", "attachment;filename="+fileName);
		try {
			List<ExportPlayData> list  = baseManager.query("Export.playData", parameters);
			out = response.getOutputStream();
			for (int i = 1; i < list.size(); i++) {
				out.write(list.get(i).toString().getBytes("utf-8"));
			}
 		} finally {
			try {
				if (out != null) {
					out.flush();
					out.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 导出用户信息
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value="/user.htm",method=RequestMethod.GET)
	public void userPlay(HttpServletResponse response,HttpServletRequest request) throws IOException{
		String begin_time = request.getParameter("begin_time");
		String end_time = request.getParameter("end_time");
		String app_name = request.getParameter("app_name");
		
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("begin_time", begin_time);
		parameters.put("end_time", end_time);
		
		OutputStream out = null;
		response.setContentType("text/plain");  
		String fileName=String.format("%s_%s_%s_user.txt", begin_time,end_time,app_name);
		response.setHeader("Content-disposition", "attachment;filename="+fileName);  
		try {
			List<ExportUserData> list  = baseManager.query("Export.userData", parameters);
			out = response.getOutputStream();
			for (int i = 1; i < list.size(); i++) {
				out.write(list.get(i).toString().getBytes("utf-8"));
			}
 		} finally {
			try {
				if (out != null) {
					out.flush();
					out.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 导出订单信息
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value="/order.htm",method=RequestMethod.GET)
	public void orderPlay(HttpServletResponse response,HttpServletRequest request) throws IOException{
		String begin_time = request.getParameter("begin_time");
		String end_time = request.getParameter("end_time");
		String app_name = request.getParameter("app_name");
		
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("begin_time", begin_time);
		parameters.put("end_time", end_time);
		
		OutputStream out = null;
		response.setContentType("text/plain");  
		String fileName=String.format("%s_%s_%s_order.txt", begin_time,end_time,app_name);
		response.setHeader("Content-disposition", "attachment;filename="+fileName);  
		try {
			List<ExportOrderData> list  = baseManager.query("Export.orderData", parameters);
			out = response.getOutputStream();
			for (int i = 1; i < list.size(); i++) {
				out.write(list.get(i).toString().getBytes("utf-8"));
			}
 		} finally {
			try {
				if (out != null) {
					out.flush();
					out.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 
	 * @Description 导出消息信息
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value="/message.htm",method=RequestMethod.GET)
	public void exportMessage(HttpServletResponse response,HttpServletRequest request) throws IOException{
		
		HSSFWorkbook wb = new HSSFWorkbook();
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/OCTET-STREAM");

        String filedisplay = "用户消息.xls";
        filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
        System.out.println(filedisplay);
        response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
        HSSFSheet sheet = wb.createSheet("用户消息");
        sheet.setColumnWidth(0, 8000);
        HSSFFont font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)36);

        HSSFRow row = sheet.createRow(0);

        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment((short)2);

        HSSFCell cell1 = row.createCell(0);
        cell1.setCellValue("发送人");
        HSSFCell cell2 = row.createCell(1);
        cell2.setCellValue("标题");
        HSSFCell cell3 = row.createCell(2);
        cell3.setCellValue("内容");
        HSSFCell cell4 = row.createCell(3);
        cell4.setCellValue("用户手机");
        HSSFCell cell5 = row.createCell(4);
        cell5.setCellValue("发送时间");
        String createMan = request.getParameter("createMan");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("endTime", endTime);
		parameters.put("startTime", startTime);
		parameters.put("createMan", createMan);
        List<Message> list  = baseManager.queryAll(Message.class, parameters);
        for (int i = 0; i < list.size(); i++) {
          row = sheet.createRow(i + 1);
          row.createCell(0).setCellValue(((Message)list.get(i)).getCreateMan());
          row.createCell(1).setCellValue(((Message)list.get(i)).getTitle());
          row.createCell(2).setCellValue(((Message)list.get(i)).getContent());
          row.createCell(3).setCellValue(((Message)list.get(i)).getPhoneNum());
          row.createCell(4).setCellValue(((Message)list.get(i)).getCreateTime());
        }

        try {
          OutputStream out = response.getOutputStream();
          wb.write(out);
          out.close();
        }
        catch (Exception e) {
          e.printStackTrace();
        }
	}
	
	
	
	/**
	 * 
	 * @Description 导出用户明细
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value="/userdetails.htm",method=RequestMethod.GET)
	public void userdetails(HttpServletResponse response,HttpServletRequest request) throws IOException{
		
		HSSFWorkbook wb = new HSSFWorkbook();
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/OCTET-STREAM");

        String filedisplay = "用户消息.xls";
        filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
        System.out.println(filedisplay);
        response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
        HSSFSheet sheet = wb.createSheet("用户消息");
        sheet.setColumnWidth(0, 8000);
        HSSFFont font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)36);

        HSSFRow row = sheet.createRow(0);

        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment((short)2);

        HSSFCell cell1 = row.createCell(0);
        cell1.setCellValue("发送人");
        HSSFCell cell2 = row.createCell(1);
        cell2.setCellValue("标题");
        HSSFCell cell3 = row.createCell(2);
        cell3.setCellValue("内容");
        HSSFCell cell4 = row.createCell(3);
        cell4.setCellValue("用户手机");
        HSSFCell cell5 = row.createCell(4);
        cell5.setCellValue("发送时间");
        String createMan = request.getParameter("createMan");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("endTime", endTime);
		parameters.put("startTime", startTime);
		parameters.put("createMan", createMan);
        List<UserDetails> list  = baseManager.queryAll(UserDetails.class, parameters);
        for (int i = 0; i < list.size(); i++) {
          row = sheet.createRow(i + 1);
          row.createCell(1).setCellValue(((UserDetails)list.get(i)).getPhoneNum());
          row.createCell(2).setCellValue(((UserDetails)list.get(i)).getPhoneType());
          row.createCell(3).setCellValue(((UserDetails)list.get(i)).getCardNum());
          row.createCell(4).setCellValue(((UserDetails)list.get(i)).getBankName());
          row.createCell(5).setCellValue(((UserDetails)list.get(i)).getCardPlace());
          row.createCell(6).setCellValue(((UserDetails)list.get(i)).getMobileNo());
          row.createCell(7).setCellValue(((UserDetails)list.get(i)).getBankState());
          row.createCell(8).setCellValue(((UserDetails)list.get(i)).getIsDefault());
          row.createCell(9).setCellValue(((UserDetails)list.get(i)).getCardType());
          row.createCell(10).setCellValue(((UserDetails)list.get(i)).getRepaymentMoney());
          row.createCell(11).setCellValue(((UserDetails)list.get(i)).getRateMoney());
          row.createCell(12).setCellValue(((UserDetails)list.get(i)).getDateWithdraw());
          row.createCell(13).setCellValue(((UserDetails)list.get(i)).getOverdueDay());
          row.createCell(14).setCellValue(((UserDetails)list.get(i)).getInstalPeriod());
          row.createCell(15).setCellValue(((UserDetails)list.get(i)).getRepaymentMoney());
          row.createCell(16).setCellValue(((UserDetails)list.get(i)).getTrueQuota());
          row.createCell(17).setCellValue(((UserDetails)list.get(i)).getWithdrawState());
          row.createCell(18).setCellValue(((UserDetails)list.get(i)).getWithdrawDay());
          row.createCell(19).setCellValue(((UserDetails)list.get(i)).getQuota());
          row.createCell(20).setCellValue(((UserDetails)list.get(i)).getComUseType());
        }

        try {
          OutputStream out = response.getOutputStream();
          wb.write(out);
          out.close();
        }
        catch (Exception e) {
          e.printStackTrace();
        }
	}
	
	/**
	 * 
	 * @Description 导出消息信息
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value="/customerEcport.htm",method=RequestMethod.GET)
	public void exportCustomer(HttpServletResponse response,HttpServletRequest request) throws IOException{
		
		HSSFWorkbook wb = new HSSFWorkbook();
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/OCTET-STREAM");

        String filedisplay = "用户消息.xls";
        filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
        System.out.println(filedisplay);
        response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
        HSSFSheet sheet = wb.createSheet("用户消息");
        sheet.setColumnWidth(0, 8000);
        HSSFFont font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)36);

        HSSFRow row = sheet.createRow(0);

        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment((short)2);

        HSSFCell cell1 = row.createCell(0);
        cell1.setCellValue("手机号");
        HSSFCell cell2 = row.createCell(1);
        cell2.setCellValue("手机型号");
        HSSFCell cell3 = row.createCell(2);
        cell3.setCellValue("注册时间");
        HSSFCell cell4 = row.createCell(3);
        cell4.setCellValue("渠道");
        HSSFCell cell5 = row.createCell(4);
        cell5.setCellValue("最近登陆时间");
        HSSFCell cell6 = row.createCell(5);
        cell6.setCellValue("用户状态");
        HSSFCell cell7 = row.createCell(6);
        cell7.setCellValue("状态");
        
        String phoneNum = request.getParameter("phoneNum");
		String phoneType = request.getParameter("phoneType");
		String createTime = request.getParameter("createTime");
		String source = request.getParameter("source");
		String viewCode = request.getParameter("viewCode");
		String state = request.getParameter("state");
		
        List<Customer> list  = baseManager.queryAll(Customer.class, null);
        for (int i = 0; i < list.size(); i++) {
          row = sheet.createRow(i + 1);
          row.createCell(0).setCellValue(((Customer)list.get(i)).getPhoneNum());
          row.createCell(1).setCellValue(((Customer)list.get(i)).getPhoneType());
          row.createCell(2).setCellValue(((Customer)list.get(i)).getCreateTime());
          row.createCell(3).setCellValue(((Customer)list.get(i)).getSource());
          row.createCell(4).setCellValue(((Customer)list.get(i)).getViewCode());
          row.createCell(4).setCellValue(((Customer)list.get(i)).getState());
        }

        try {
          OutputStream out = response.getOutputStream();
          wb.write(out);
          out.close();
        }
        catch (Exception e) {
          e.printStackTrace();
        }
	}
	
	
	
	
	
	
	
	
	/**
	 * 
	 * @Description 导出消息信息
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value="/ditchNameEcport.htm",method=RequestMethod.GET)
	public void exportDitchName(HttpServletResponse response,HttpServletRequest request) throws IOException{
		HSSFWorkbook wb = new HSSFWorkbook();
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/OCTET-STREAM");

        String filedisplay = "渠道统计报表.xls";
        filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
        System.out.println(filedisplay);
        response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
        HSSFSheet sheet = wb.createSheet("渠道统计报表");
        sheet.setColumnWidth(0, 8000);
        HSSFFont font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)36);

        HSSFRow row = sheet.createRow(0);

        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment((short)2);

        HSSFCell cell1 = row.createCell(0);
        cell1.setCellValue("客户信息");
        HSSFCell cell2 = row.createCell(1);
        cell2.setCellValue("统计数量");
        
        List<DitchName> list  = baseManager.query(DitchName.class, null);
        for (int i = 0; i < list.size(); i++) {
          row = sheet.createRow(i + 1);
          row.createCell(0).setCellValue(((DitchName)list.get(i)).getDistchName());
          row.createCell(1).setCellValue(((DitchName)list.get(i)).getCountNum());
        }

        try {
          OutputStream out = response.getOutputStream();
          wb.write(out);
          out.close();
        }
        catch (Exception e) {
          e.printStackTrace();
        }
	}
}
